﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace IPrinting
{
    public class PrintDataPackge
    {
        /// <summary>
        /// POST输入参数的动态对象
        /// </summary>
        private JObject _params = new JObject();

        /// <summary>
        /// 调用接口的名称
        /// </summary>
        public string Invoker
        {
            get;
            set;
        }

        /// <summary>
        /// 执行的动作名称
        /// </summary>
        public string Action
        {
            get;
            set;
        }
       
        /// <summary>
        /// 输入参数，通常和_params作为联动对象出现
        /// </summary>
        public string InParams
        {
            get
            {
                return _params.ToString();
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    _params = new JObject();
                }
                else
                {
                    _params = JObject.Parse(value);
                }
            }
        }

        /// <summary>
        /// 获取或者设置输入或输出参数选项
        /// </summary>
        /// <param name="key">0，表示输入参数；1(!=0)，表示输出参数</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public string this[string key]
        {
            get
            {
                if (_params == null)
                {
                    return "";
                }
                try
                {
                    // 欲获取输出参数
                    return _params[key].ToString();
                }
                catch
                {
                    return "";
                }
            }
            set
            {
                Add(key, value);
            }
        }


        /// <summary>
        /// 默认构造函数
        /// </summary>
        public PrintDataPackge()
        {
            
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public PrintDataPackge(string name)
        {
            Invoker = name;
        }

        /// <summary>
        /// 通过Json对象赋值初始化对象
        /// </summary>
        /// <param name="json"></param>
        public void SetParams(JObject param)
        {
            _params = param;
            InParams = _params.ToString();
        }

        /// <summary>
        /// 将Json字符串转化为Json对象
        /// </summary>
        public void SetParams(string jsonString)
        {
            InParams = jsonString;
            if (string.IsNullOrEmpty(jsonString))
            {
                _params = new JObject();
            }
            else
            {
                _params = JObject.Parse(jsonString);
            }
        }

        public void Add(string key, string value)
        {
            if (_params == null)
            {
                _params = new JObject();
            }
            _params[key] = value;
            InParams = _params.ToString();
        }
    }
}
